<?php
// +----------------------------------------------------------------------
// | [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 机油
// +----------------------------------------------------------------------
namespace app\ymkj\controller;

use app\model\Order as orderModel;
use think\facade\Session;
use app\model\JiyouLog;
use think\Controller;
use think\Db;
 
class Jiyou extends Controller
{
  public function index()
  {
    $data = request()->param();
    $jiyou = Db::name('jiyou')->where('status','=',1)->select();
    $xiche = Db::name('xiche')->where('id','=',$data['xid'])->find();
    $this->assign([
      'jiyou' => $jiyou,
      'xiche' => $xiche,
    ]);
    return view();
  }

  public function item()
  {
    $data = request()->param();
    $jiyou = Db::name('jiyou')->where('id','=',$data['jid'])->find();
    $xiche = Db::name('xiche')->where('id','=',$data['xid'])->find();
    $this->assign([
      'jiyou' => $jiyou,
      'xiche' => $xiche,
    ]);
    return view();
  }

  public function xiadan()
  {
    $data = request()->param();
    $order = new orderModel();
    $user = Session::get('wewhat_user');
    $jine = Db::name('jiyou')->where('id','=',$data['jid'])->value('huiyuan');
    $jines = $jine*$data['num'];
    $jilog_id = $this->rizhi($data,$jines); // 机油日志id
    $createOrder = $order->save([
      'shop_id' => $data['xid'],
      'openid' => $user['openid'],
      'name' => "机油保养",
      'jine' => $jine,
      'jid' => $jilog_id
    ]);
    if( $createOrder ){
      return redirect('Buy/index',array('order_id'=>$order->id));
      //return redirect('Wechat1/save')->with(['name'=>"机油保养",'order_id'=>$order->id,'price'=>$jines,'openid'=>$user['openid']]);
    } else {
      $this->error('下单失败');
    }
  }

  // 机油日志
  public function rizhi($data,$jines)
  {
    $model = new JiyouLog();
    $user = Session::get('wewhat_user');
    $result = $model->save([
      'xid' => $data['xid'],
      'jid' => $data['jid'],
      'num' => $data['num'],
      'openid' => $user['openid'],
      'jiner' => $jines,
    ]);
    return $model->id;
  }
}
?>